package 华为;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangmin
 * @create 2022-09-19 8:50
 */
public class Main_0919_1 {
    public static void main(String[] args) {
        String line="-x+3x=4";
        String right=line.split("=")[1];
        int r=Integer.parseInt(right);
        String left=line.split("=")[0];
        char[] arrl=left.toCharArray();
        int r_cc=0;//存放等式右边放常量和
        String temp="";
        List<String> x_list=new ArrayList<>();
        for (int i = 0; i < arrl.length; i++) {
            if (arrl[i]=='-'){
                if (temp.contains("x")){
                    x_list.add(temp);
                    temp="-";
                }else {
                    if (temp!=""){
                        r_cc=r_cc-Integer.parseInt(temp);
                        temp="-";
                    }else {
                        temp="-";
                    }
                }
            }else if (arrl[i]=='+'){
                if (temp.contains("x")){
                    x_list.add(temp);
                    temp="+";
                }else {
                    if (temp!=""){
                        r_cc=r_cc-Integer.parseInt(temp);
                        temp="+";
                    }else {
                        temp="+";
                    }
                }
            }else {
                temp=temp+arrl[i];//不包含，直接加入
            }
        }
        if (!temp.equals("")){
            if (temp.contains("x")){
                x_list.add(temp);
            }else {
                r_cc+=Integer.parseInt(temp);
            }
        }
        int left_cc=0;//处理左边的x前缀
        for (int i = 0; i < x_list.size(); i++) {
            String x1=x_list.get(i);
            x1=x1.replaceAll("x","");
            if (x1.contains("+")){
                if (x1.equals("+")){
                    x1="1";
                }
            }else if (x1.contains("-")){
                if (x1.equals("-")){
                    x1="-1";
                }
            }else {
                if (x1.equals("")){
                    x1="1";
                }
            }
            left_cc+=Integer.parseInt(x1);
        }
        System.out.println("r="+r);
        System.out.println("left_cc="+left_cc);
        System.out.println("r_cc="+r_cc);
        float res=(r-r_cc)/left_cc;
        System.out.println(String.format("x=%.2f",res));
    }
}
